package com.koi.common.utils.number;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 生成订单号 工具类
 */
public class GenOrderUtil {

    // 17位
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    /**
     * 随机数
     *
     * @param place 定义随机数的位数
     */
    public static String randomGenLetter(int place) {
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer sb = new StringBuffer();
        Random rd = new Random();
        for (int i = 0; i < place; i++) {
            sb.append(base.charAt(rd.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 生成订单号，微信订单号指定32位
     *
     * @param pre 订单号前缀
     * @return
     */
    public static String genNumber(String pre, Integer number) {
        //生成
        String orderNo = pre + "-" + sdf.format(new Date()) + "-" + GenOrderUtil.randomGenLetter(number);
        return orderNo;
    }

    /**
     * @description [生成时间戳]
     * @author ･ᴗ･
     * @method genTimeNumber
     */
    public static String genTimeNumber(){
        return sdf.format(new Date());
    }

    public static void main(String[] args) {
        // 32位数
        String orderNo = GenOrderUtil.genNumber("KOI", 10);
        System.out.println("订单号->" + orderNo);
        String timeNumber = GenOrderUtil.genTimeNumber();
        System.out.println("时间戳->" + timeNumber);
    }

}
